<template>
  <el-form-item :label="label" :prop="prop" :label-width="labelWidth" :rules="rules">
    <slot />
  </el-form-item>
</template>

<script>
export default {
  props: {
    prop: {
      type: String,
      default: ""
    },
    label: {
      type: String,
      default: ""
    },
    required: {
      type: Boolean,
      default: true
    },
    labelWidth: {
      type: String,
      default: ""
    },
    errorTip: {
      type: String,
      default: "不能为负数"
    },
    requiredMessage: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      rules: [
        { required: this.required, message: "请填写" + this.requiredMessage, trigger: "blur" },
        {
          validator: (rule, value, callback) => {
            if (+value < 0) {
              callback(new Error(this.errorTip));
            } else {
              callback();
            }
          },
          trigger: "blur",
        },
      ],
    };
  },
};
</script>
